(function(){

'use strict';

angular.module('controllers.oldOMDisplay.infoDispaly')
  .controller('operationAnalysisController', operationAnalysisController);

function operationAnalysisController($scope, InfoViewService, COREDATA,CoreLoading,CoreService) {
  //无请求写死的数据 时间跨度下拉数据
  $scope.timeTypes = [ {timeTypeCode : '2' ,timeTypeName: '周'},{timeTypeCode : '3' ,timeTypeName: '月'},{timeTypeCode : '4' ,timeTypeName: '季'}];
  //省份数据
  $scope.provinces = COREDATA.PROVINCES;
	//业务线
	$scope.businessList = COREDATA.BUSINESSLINE;

  $scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息

	$scope.shoumun = 1;
  //查询条件参数
	$scope.translate = {
		provinceCode: [],
		timeTypeCode: {timeTypeCode: '2',timeTypeName: '周'},
		startTime: new Date(),
		startQuarter: { code: '1', quarterName: '第一季度'},
		endTime: new Date(),
		endQuarter: { code: '1', quarterName: '第一季度'},
		year: {}
	};

	/**
	 * [ 设置账期起始时间的默认值]
	 * 初始化页面值的默认 查询账期为日
	 * @param {[type]} timeTypeCode [description]
	 */
	$scope.$watch('translate.timeTypeCode',function(newValue){
		var month = new Date().getMonth();                  //当前月份
		var day = new Date().getDate();                     //当前天
		var year = new Date().getFullYear();                //当前年份
		switch (newValue.timeTypeCode) {
			case '2':
				$scope.translate.startTime = new Date(year, month, day - 1);
				$scope.translate.endTime = new Date(year, month, day - 1);
				break;
			case '3':
				$scope.translate.startTime = new Date(year, month);
				$scope.translate.endTime = new Date(year, month);
				break;
			case '4':
				$scope.translate.year.startTime = '';
				$scope.translate.year.endTime = '';
				break;
		}
	});

  $scope.queryAllChannel = function() {
	  var paramsData = {};
    paramsData.provinceCodes = $scope.translate.provinceCode.map(function(item){
      return item.provinceCode;
    });
    paramsData.timeType = $scope.translate.timeTypeCode.timeTypeCode;
	  $scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
	  if($scope.shoumun === 0){
      CoreLoading.setState(true);
		  InfoViewService.queryAllChannel(paramsData, queryAllChannel);
	  }
  };
 
  var channelName, businessNum,operateIncome,average;

  function queryAllChannel(res) {
    CoreLoading.setState(false);
    if(res.state === '10000'){
      $scope.flagObj = true;
      channelName = [];
      businessNum = [];
      operateIncome = [];
      average = [];
      //遍历出所有的图标数据
      angular.forEach(res.data, function (channel){
        channelName.push(channel.channelName);
        businessNum.push(channel.businessNum);
        operateIncome.push(channel.operateIncome);
        average.push(channel.average);
      });

      var itemStyle =  {
                 normal: { // 系列级个性化，横向渐变填充
                   borderRadius: 5
                 }
               };
      var option = {
          title: {
              text: '渠道总运营分析',
              x: 'center'
          },
          tooltip: {
              trigger: 'axis',
              axisPointer: {
                  type: 'shadow'
              }
          },
          legend: {
              orient: 'vertical',
              x: 'right',
              y: 'center',
              data: ['业务受理量(笔)','营业收入(元)','平均收入(元/笔)']
          },
          // calculable: true,
          grid: {
            x2: '13%'
          },
          xAxis: [
              {
                  type: 'category',
                  data: channelName
              }
          ],
          yAxis: [
              {
                  type: 'value'
              }
          ],
          series: [
              {
                  name: '业务受理量(笔)',
                  type: 'bar',
                  itemStyle: itemStyle,
                  data: businessNum
              },
              {
                  name: '营业收入(元)',
                  type: 'bar',
                  itemStyle: itemStyle,
                  data: operateIncome
              },
              {
                  name: '平均收入(元/笔)',
                  type: 'bar',
                  itemStyle: itemStyle,
                  data: average
              }
          ]
      };
                    

      var customerEchart = echarts.init(document.getElementById('operation')); 

      customerEchart.setOption(option);
    }else{
      CoreService.dialog(res.state);
    }
   
  }
  $scope.queryAllChannel();
}
})();